View Javadoc
1 //============================================================================== 2 // file : JDOContext.java 3 // project: Java Common Utility 4 // 5 // last change: date: $Date: 2003/09/10 09:22:14 $ 6 // by: $Author: bitiboy $ 7 // revision: $Revision: 1.1 $ 8 //------------------------------------------------------------------------------ 9 // copyright: GNU GPL Software License (see class documentation) 10 //============================================================================== 11 package com.justhis.jdo; 12 13 14 /* 15 * $Id: JDOContext.java,v 1.1 2003/09/10 09:22:14 bitiboy Exp $ 16 * 17 * Copyright 2003 Acai Software All Rights Reserved. 18 * 19 * This file JDOContext.java is part of the Java Common Utility. 20 21 * The Java Common Utility is free software; you can redistribute it and/or modify 22 * it under the terms of the GNU General Public License as published by 23 * the Free Software Foundation; either version 2 of the License, or 24 * (at your option) any later version. 25 26 * Java Common Utility is distributed in the hope that it will be useful, 27 * but WITHOUT ANY WARRANTY; without even the implied warranty of 28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 29 * GNU General Public License for more details. 30 31 * You should have received a copy of the GNU General Public License 32 * along with the Java Common Utility; if not, write to the Free Software 33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 34 35 * http://www.justhis.com http://ejb.cn 36 * CONTACT: email = webmaster@justhis.com superaxis@sohu.com 37 */ 38 import com.justhis.util.PropertiesReader; 39 import com.justhis.util.exception.PropertiesFileReadException; 40 41 import java.util.Properties; 42 43 import javax.jdo.JDOHelper; 44 import javax.jdo.PersistenceManager; 45 import javax.jdo.PersistenceManagerFactory; 46 47 48 /*** 49 * JDO????????????????JDO PersistenceManager ????Factory 50 * 51 * @author <a href="http://blog.ejb.cn">acai</a> 52 * @version $Revision: 1.1 $ 53 */ 54 public class JDOContext { 55 //~ Static fields/initializers --------------------------------------------- 56 57 /*** ????JDO???????? */ 58 private static PersistenceManagerFactory pmf; 59 60 /*** ????????,????????????JDOfactory */ 61 private static ThreadLocal contextHolder = new ThreadLocal(); 62 63 //~ Instance fields -------------------------------------------------------- 64 65 /*** ???????????? */ 66 private PersistenceManager pm; 67 68 //~ Constructors ----------------------------------------------------------- 69 70 /*** 71 * Contstructor 72 * 73 * @throws PropertiesFileReadException ???????????????????? 74 */ 75 private JDOContext() throws PropertiesFileReadException { 76 initialize(loadJDOProperties()); //??????JDO?????????? 77 } 78 79 //~ Methods ---------------------------------------------------------------- 80 81 /*** 82 * Obtain instance of the Context for current thread. 83 * 84 * @return Current thread's context 85 * 86 * @throws PropertiesFileReadException if read JDO properties file happen 87 * error. 88 */ 89 public static JDOContext getContext() throws PropertiesFileReadException { 90 JDOContext ctx = (JDOContext) contextHolder.get(); 91 92 if (ctx == null) { 93 contextHolder.set(ctx = new JDOContext()); 94 } 95 96 return ctx; 97 } 98 99 /*** 100 * Creates and configures {@link PersistenceManagerFactory}. 101 * 102 * @param props JDO configuration parameters 103 */ 104 public static void initialize(Properties props) { 105 pmf = JDOHelper.getPersistenceManagerFactory(props); 106 } 107 108 /*** 109 * Load the JDO configuration from the classpath. 110 * 111 * @return PropertiesFileReadException if load properties error. 112 */ 113 public static Properties loadJDOProperties() 114 throws PropertiesFileReadException { 115 Properties props = PropertiesReader.getProperties("/jcredo"); 116 117 return props; 118 } 119 120 /*** 121 * Shut down JDO engine 122 */ 123 public static void shutdown() { 124 // non-portable code with JDO 1.01 this will be replaced by pmf.close() 125 } 126 127 /*** 128 * Obtain this context's JDO persistence manager. 129 * 130 * @return JDO persistence manager 131 */ 132 public PersistenceManager getPersistenceManager() { 133 if (pm == null) { 134 pm = pmf.getPersistenceManager(); 135 } 136 137 return pm; 138 } 139 140 /*** 141 * Close Context's instance. Closes its instance of {@link 142 * PersistenceManager} and removes reference to this instance from current 143 * thread. 144 */ 145 public void close() { 146 contextHolder.set(null); 147 148 if (pm != null) { 149 /*pm.currentTransaction().begin(); 150 pm.refreshAll(); 151 pm.currentTransaction().commit(); 152 pm.refreshAll();*/ 153 pm.close(); 154 pm = null; 155 } else if (pmf != null) { 156 } 157 } 158 } 159 160 161 /* 162 * $Log: JDOContext.java,v $ 163 * Revision 1.1 2003/09/10 09:22:14 bitiboy 164 * *** empty log message *** 165 * 166 * 167 */

This page was automatically generated by Maven